package com.xu.cloud.common.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.xu.cloud.common.constant.SystemConstant;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * MybatisPlus代码生成器
 *
 * @author xuguan
 * @since 2023/3/1
 */
public class MybatisPlusGeneratorUtil {

    public static void generate(MPGDto mpgDto) {
        String author = mpgDto.getAuthor();
        String datePattern = mpgDto.getDatePattern();
        String url = mpgDto.getUrl();
        String username = mpgDto.getUsername();
        String password = mpgDto.getPassword();
        List<String> tableNames = mpgDto.getTableNames();
        String outDir = mpgDto.getOutDir();

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .commentDate(datePattern)
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outDir); // 指定输出目录

                })
                .packageConfig(builder -> {
                    builder.parent("com") // 设置父包名
                            .moduleName("generate") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, outDir + "/xml")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableNames) // 设置需要生成的表名
                            .addTablePrefix("t_", "c_") // 设置过滤表前缀

                            .entityBuilder()
                            .enableRemoveIsPrefix() // 移除is前缀, 视情况开启
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .addTableFills(new Column("create_time", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.INSERT_UPDATE)) // mybatisplus自动注入
                            .versionColumnName("version_lock")// 乐观锁
                            .formatFileName("%sEntity")

                            .mapperBuilder()
                            .enableMapperAnnotation()
                            .formatMapperFileName("%sMapper")

                            .formatXmlFileName("%sMapper")
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    @Data
    public static class MPGDto {
        // 注释信息
        private String author = "xuguan";
        private String datePattern = "yyyy/MM/dd";

        // 数据库相关
        private String url = "jdbc:mysql://localhost:3306/xucloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";
        private String username = "root";
        private String password = "123456";

        // 需要自动生成的表名
        private List<String> tableNames = new ArrayList<>();

        // 代码生成目录
        private String outDir = SystemConstant.USER_HOME + SystemConstant.SYSTEM_FILE_SEP + "Downloads";

        public void addTableName(String tableName) {
            tableNames.add(tableName);
        }
    }

    public static void main(String[] args) {
        final MPGDto mpgDto = new MPGDto();
        generate(mpgDto);
    }
}
